//
//  UserViewController.m
//  Meroad
//
//  Created by 郭孝通 on 2019/2/16.
//  Copyright © 2019 郭孝通. All rights reserved.
//

#import "UserViewController.h"
#import "AccountViewController.h"
#import "editViewController.h"
#import "UserModel.h"
#import "AppDelegate.h"

@interface UserViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
    BOOL alreadylogin;
}

@property (nonatomic) NSMutableArray* informationArray;
@property (nonatomic) NSMutableArray* textArray;
@property (nonatomic) NSMutableArray* imageArray;

@property (weak, nonatomic) IBOutlet UIImageView *userIcon;
- (IBAction)logoutAction:(UIButton *)sender;

- (IBAction)editInfo:(id)sender;

@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self setCirclePhoto];
    
    //tableView中依次显示如下的image和text
    _imageArray = [NSMutableArray array];
    _textArray = [NSMutableArray array];
    _informationArray = [NSMutableArray array];
    
    
    _userID.text = self.userModel.userName;
    _userID.textAlignment = NSTextAlignmentCenter;
    _userID.textColor = [UIColor whiteColor];
    
    NSString *str = [NSString stringWithFormat:@"性别"];
    [_textArray addObject:str];
    UIImage *image = [UIImage imageNamed:@"性别-深灰"];
    [_imageArray addObject:image];
    
    str = [NSString stringWithFormat:@"年龄"];
    [_textArray addObject:str];
    image = [UIImage imageNamed:@"年龄-深灰"];
    [_imageArray addObject:image];
    
    str = [NSString stringWithFormat:@"我的邮箱"];
    [_textArray addObject:str];
    image = [UIImage imageNamed:@"我的邮箱-深灰"];
    [_imageArray addObject:image];
    
    str = [NSString stringWithFormat:@"我的电话"];
    [_textArray addObject:str];
    image = [UIImage imageNamed:@"我的电话-深灰"];
    [_imageArray addObject:image];
    
    str = [NSString stringWithFormat:@"我的信用"];
    [_textArray addObject:str];
    image = [UIImage imageNamed:@"我的信用-深灰"];
    [_imageArray addObject:image];
    
    str = [NSString stringWithFormat:@"问题审核"];
    [_textArray addObject:str];
    image = [UIImage imageNamed:@"问题审核-深灰"];
    [_imageArray addObject:image];
    
    str = [NSString stringWithFormat:@"联系客服"];
    [_textArray addObject:str];
    image = [UIImage imageNamed:@"联系客服-深灰"];
    [_imageArray addObject:image];

}

- (void)viewWillAppear:(BOOL)animated{
    NSLog(@"View will appear");
    
    _informationArray = [self removeTheWholeArray:_informationArray];
    NSString *str = [NSString stringWithFormat:@"%@",self.userModel.userName];
    [_informationArray addObject:str];
    
    str = [NSString stringWithFormat:@"%@",self.userModel.userGender];
    [_informationArray addObject:str];
    
    str = [NSString stringWithFormat:@"%@",self.userModel.userAge];
    [_informationArray addObject:str];
    
    str = [NSString stringWithFormat:@"%@",self.userModel.userEmail];
    [_informationArray addObject:str];
    
    str = [NSString stringWithFormat:@"%@",self.userModel.userTel];
    [_informationArray addObject:str];

    str = [NSString stringWithFormat:@"%@",self.userModel.userCredit];
    [_informationArray addObject:str];
    
    str = [NSString stringWithFormat:@"%@",self.userModel.userCredit];
    [_informationArray addObject:str];
    
    _userID.text = [_informationArray objectAtIndex:0];
    
    [self.tableView reloadData];
}

#pragma mark 代理

//返回多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

//每一组返回多少行
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0) return 4;
    if(section == 1) return 3;
    return 0;
}

//每一行显示数据
//UITableViewCell：每一行的格子
//indexPath：代表唯一的一行
//indexPath.section：获取对应的组号
//indexPath.row：获取行号
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //第几个cell
    NSInteger num = indexPath.section * 4 + indexPath.row;
    //建立标志符字符串
    static NSString* ID = @"Cell";
    //让cell从缓存池中出来
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1   reuseIdentifier:ID];
        [cell.imageView setImage:[_imageArray objectAtIndex:num]];
        [cell.imageView setFrame:CGRectMake(5, 5, (40-5*2), (40-5*2))];
        [cell.textLabel setText:[_textArray objectAtIndex:num]];
        
        [cell.textLabel setFont:[UIFont systemFontOfSize:20]];
        
        if(num < 5){
            
            [cell.detailTextLabel setText:[_informationArray objectAtIndex:num + 1]];
        }
        
    }
    else{
        if(num < 5){
            [cell.detailTextLabel setText:[_informationArray objectAtIndex:num + 1]];
        }
    }
    
    
    if(indexPath.section == 0){
        [cell setAccessoryType:UITableViewCellAccessoryNone];
        cell.userInteractionEnabled = NO;
    }
    else{
        [cell setAccessoryType:UITableViewCellAccessoryNone];
        cell.userInteractionEnabled = YES;
    }
    
    return cell;
}

//返回头部文字
-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
    return NULL;
}

//返回尾部文字
-(NSString*)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section{
    if(section == 1){
        return @"MeRoad Interconnect";
    }
    else{
        return NULL;
    }
}

//设置某一行cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}


//section头部间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 33;//section头部高度
}

//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return NULL;
}

//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if(section == 1){
        return 60;
    }
    return 10;
}

//section底部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return NULL;
}


//设置cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //当手指离开某行时，就让某行的选中状态消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSInteger num = indexPath.section * 4 + indexPath.row;
    
    UIAlertController *alert;
    
    
    switch (num) {
        case 0:
            break;
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        default:
            alert = [UIAlertController alertControllerWithTitle:@"产品demo" message:@"该功能暂未上线" preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
            [self presentViewController:alert animated:true completion:nil];
            return;
    }
    //弹出提示框；
    [self presentViewController:alert animated:true completion:nil];
    
}





#pragma mark 重写get方法(懒加载)

- (UserModel *)userModel{
    if (_userModel == nil) {
        AppDelegate *_rootDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        _userModel = _rootDelegate.userModel;
    }
    return _userModel;
}



#pragma mark 头像

- (void)setCirclePhoto{
    self.userIcon.layer.cornerRadius = self.userIcon.bounds.size.width / 2;
    self.userIcon.layer.masksToBounds = true;
    
    self.userIcon.layer.borderWidth = 1;
    self.userIcon.layer.borderColor = [[UIColor whiteColor] CGColor];
    
    //设置图片
    //self.userIcon.layer.contents = (id)[[UIImage imageNamed:@"userIcon"] CGImage];
    
    self.userIcon.image = self.userModel.userHeaderImage;
    
    self.userIcon.contentMode = UIViewContentModeScaleAspectFill;
    
    self.userIcon.userInteractionEnabled = YES;
    
    [self.view bringSubviewToFront:self.userImage];
}

- (IBAction)selectUserIcon:(id)sender {
    //创建图片选择器
    UIImagePickerController *pickerControll = [[UIImagePickerController alloc] init];
    pickerControll.delegate = self;
    pickerControll.editing = YES;
    pickerControll.allowsEditing = YES;
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请选择打开" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"照相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action){
        
        pickerControll.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        [self presentViewController:pickerControll
                           animated:YES
                         completion:nil];
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action){
        
        pickerControll.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        
        [self presentViewController:pickerControll
                           animated:YES
                         completion:nil];
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action){
        //取消
    }]];
    //弹出提示框
    [self presentViewController:alert animated:true completion:nil];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerController*,id> *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    self.userIcon.image = image;
    self.userModel.userHeaderImage = image;
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark others

-(NSMutableArray *)removeTheWholeArray:(NSMutableArray *)array{
    if([array count]){
        [array removeAllObjects];
        return array;
    }else{
        return array;
    }
}

#pragma mark action

- (IBAction)editInfo:(id)sender {
    EditViewController *vc = [[EditViewController alloc] init];
    [self presentViewController:vc
                       animated:YES
                     completion:nil];
}

- (IBAction)logoutAction:(UIButton *)sender {
    AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    appDelegate.userModel.userName = nil;
    appDelegate.userModel.cookie = nil;
    AccountViewController *accountVC = [[AccountViewController alloc] init];
    [self presentViewController:accountVC animated:YES completion:nil];
}

@end
